package cn.edu.sgu.www.mhxysy.controller.baoshi;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.baoshi.BaoshiDTO;
import cn.edu.sgu.www.mhxysy.dto.baoshi.EquipmentBaoshiDTO;
import cn.edu.sgu.www.mhxysy.entity.baoshi.Baoshi;
import cn.edu.sgu.www.mhxysy.pager.baoshi.BaoshiPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.baoshi.BaoshiService;
import cn.edu.sgu.www.mhxysy.vo.baoshi.BaoshiVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "宝石管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/baoshi", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class BaoshiController {

	private final BaoshiService baoshiService;

	@Autowired
	public BaoshiController(BaoshiService baoshiService) {
		this.baoshiService = baoshiService;
	}

	@ApiOperation("宝石镶嵌")
	@RequestMapping(value = "/inlay", method = RequestMethod.POST)
	public JsonResult<Void> inlay(@Validated EquipmentBaoshiDTO equipmentBaoshiDTO) {
		baoshiService.inlay(equipmentBaoshiDTO);

		Integer inlayStatus = equipmentBaoshiDTO.getInlayStatus();
		String[] operationArray = {"镶嵌", "取下"};

		return JsonResult.success(operationArray[inlayStatus] + "成功");
	}

	@ApiOperation("添加宝石")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated BaoshiDTO baoshiDTO) {
		baoshiService.insert(baoshiDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除宝石")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "宝石ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		baoshiService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改宝石信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated BaoshiDTO baoshiDTO) {
		baoshiService.updateById(baoshiDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("通过ID查询宝石信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Baoshi> selectById(
			@ApiParam(name = "id", value = "宝石ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		Baoshi baoshi = baoshiService.selectById(id);

		return JsonResult.success(null, baoshi);
	}

	@ApiOperation("分页查询宝石列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Baoshi>> selectByPage(BaoshiPager pager) {
		Page<Baoshi> page = baoshiService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

	@ApiOperation("查询装备镶嵌的宝石")
	@RequestMapping(value = "/selectDetails", method = RequestMethod.GET)
	public JsonResult<List<BaoshiVO>> selectImages(
			@ApiParam(name = "equipmentId", value = "装备ID", required = true)
			@NotBlank(message = CustomMessages.EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		List<BaoshiVO> list = baoshiService.selectDetails(equipmentId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("查询装备镶嵌的宝石信息")
	@RequestMapping(value = "/selectByEquipmentId", method = RequestMethod.GET)
	public JsonResult<List<BaoshiVO>> selectByEquipmentId(
			@ApiParam(name = "equipmentId", value = "装备ID", required = true)
			@NotBlank(message = CustomMessages.EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		List<BaoshiVO> list = baoshiService.selectByEquipmentId(equipmentId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("查询宝石列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = false),
			@ApiImplicitParam(name = "equipmentId", value = "装备ID", required = false)
	})
	@RequestMapping(value = "/selectList", method = RequestMethod.GET)
	public JsonResult<List<BaoshiVO>> selectList(
			@RequestParam(required = false) String roleId,
			@RequestParam(required = false) String equipmentId) {
		List<BaoshiVO> list = baoshiService.selectList(roleId, equipmentId);

		return JsonResult.success(null, list);
	}

}